/**
 * 作者：陈文斌 on 2018/12/27.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:flutter/material.dart';
import 'package:crm/constant/constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:crm/tools/shared_preferences_util.dart';
import 'package:crm/tools/apollo_util.dart';

import 'package:crm/page/Login/login.dart';
import 'package:crm/page/Login/crm_login_page.dart';
import 'package:crm/page/SettingPassword/crm_setting_gesture_password_page.dart';

const Color SelectNavigationBarItemColor = Color(0xFF3C86F3);
const Color UnSelectNavigationBarItemColor = Color(0xFFBFBFBF);
const TextStyle SelectNavigatorBarTitleStyle = TextStyle(
  color: SelectNavigationBarItemColor,
  fontSize: 10,
  fontFamily: AppFontFamilies.PingFangSC,
  fontWeight: FontWeight.w500,
);
const TextStyle UnSelectNavigationBarTitleStyle = TextStyle(
  color: UnSelectNavigationBarItemColor,
  fontSize: 10,
  fontFamily: AppFontFamilies.PingFangSC,
  fontWeight: FontWeight.w700,
);

const List<String> _navigationTitle = [
  '消息',
  '分类',
  '首页',
  '公司',
  '我的',
];
const List<String> _navigationIcon = [
  'images/navigation/home_ic_message_normal.png',
  'images/navigation/home_ic_classify_normal.png',
  'images/navigation/home_ic_home_normal.png',
  'images/navigation/home_ic_company_normal.png',
  'images/navigation/home_ic_me_normal.png',
];
final List<Widget> _navigationPage = [
  Center(child: Text('消息')),
  Center(child: Text('分类')),
  Center(child: Text('首页')),
  Center(child: Text('公司')),
  Center(child: Text('我的')),
];

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  // NavigationBar默认选中第2项
  int _tabIndex = 2;

  // 获取title
  Text getNavigationTitleAt(index) {
    return Text(
      _navigationTitle[index],
      style: index == _tabIndex
          ? SelectNavigatorBarTitleStyle
          : UnSelectNavigationBarTitleStyle,
    );
  }

  // 获取Icon
  Widget getNavigationIconAt(index) {
    return Image.asset(
      _navigationIcon[index],
      color: index == _tabIndex
          ? SelectNavigationBarItemColor
          : UnSelectNavigationBarItemColor,
      fit: BoxFit.fill,
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
    return ScreenUtilInit(
      designSize: Size(375, 667),
      builder: () => Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: AppColors.PageBackgroundColor,
        body: _navigationPage[_tabIndex],
        bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: getNavigationIconAt(0),
              title: getNavigationTitleAt(0),
            ),
            BottomNavigationBarItem(
              icon: getNavigationIconAt(1),
              title: getNavigationTitleAt(1),
            ),
            BottomNavigationBarItem(
              icon: getNavigationIconAt(2),
              title: getNavigationTitleAt(2),
            ),
            BottomNavigationBarItem(
              icon: getNavigationIconAt(3),
              title: getNavigationTitleAt(3),
            ),
            BottomNavigationBarItem(
              icon: getNavigationIconAt(4),
              title: getNavigationTitleAt(4),
            ),
          ],
          type: BottomNavigationBarType.fixed,
          currentIndex: _tabIndex,
          onTap: (index) {
            setState(() {
              _tabIndex = index;
              switch (index) {
                case 3:
                  {
                    bool isLogin = SpUtil.getBool('isLogin');
                    if (isLogin != null && isLogin) {
                      // 判断是否设置过密码
                      ApolloRequestUtil.isInitPassword((response) {
                        if (response["data"]["checkStatus"]["isInitPassword"]) {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => CRMLoginPage()),
                          );
                        }
                      }, errorCallback: (errMsg) {
                        Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => LoginPage()),
                        );
                      });
                    } else {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => CRMLoginPage()),
                      );
                    }
                  }
                  break;
              }
            });
          },
        ),
      ),
    );
  }
}
